package com.bringway.p2p.controller;

import java.io.OutputStream;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.apache.poi2.hssf.record.formula.functions.False;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.bringway.framework.controller.JsonController;
import com.bringway.framework.excel.ExcelOperUtil;
import com.bringway.framework.excel.ExcelSheet;
import com.bringway.framework.util.DateUtils;
import com.bringway.framework.util.RequestUtils;
import com.bringway.global.domain.SysUserDetails;
import com.bringway.p2p.service.JkglService;
import com.bringway.p2p.service.YzmService;
import com.bringway.p2p.util.MD5Util;
import com.bringway.p2p.util.MsgUtils;
import com.bringway.p2p.util.NumberUtil;

/**
 * 
 * @author js
 * @version 创建时间：2016年6月20日
 * @功能：借款管理界面
 */

@Controller
@RequestMapping("/jkgl")
public class JkglController extends JsonController {

	Logger logger = Logger.getLogger(PosController.class);

	@Autowired
	private JkglService jkglService;
	@Autowired
	private YzmService yzmService;

	/**
	 * 
	 * @param request
	 *            查询借款人本人发放的债权明细
	 * @param response
	 *            查询功能
	 * @return
	 * @throws Exception
	 */

	@RequestMapping("/queryJkglHkList")
	public ModelAndView queryJkglHkList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		SysUserDetails sysUserDetails = (SysUserDetails) SecurityContextHolder.getContext().getAuthentication()
				.getPrincipal();
		String dfrid = null;
		if (null != sysUserDetails) {
			dfrid = sysUserDetails.getGyh();
		}
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		Integer page = RequestUtils.getIntParameter(request, "page");// 新版本,传参
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		String xmmct = RequestUtils.getStringParameter(request, "xmmct");
		String jkrt = RequestUtils.getStringParameter(request, "jkrt");
		String state = RequestUtils.getStringParameter(request, "state");
		String xmtype = RequestUtils.getStringParameter(request, "xmtype");
		String zqid = RequestUtils.getStringParameter(request, "zqid");
		if (null != page) {// 新版本,传参
			start = (page - 1) * limit;
		}
		params.put("start", start);
		params.put("limit", limit);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("state", state);
		params.put("jkrt", jkrt);
		params.put("xmmct", xmmct);
		params.put("dfrid", dfrid);
		params.put("xmtype", xmtype);
		params.put("zqid", zqid);
		List<?> list = jkglService.queryJkglHkList(params);
		int count = jkglService.getJkglHkCount(params);
		result.put("data", list);
		result.put("total", count);
		if (null != page) {// 新版本,传参
			int totalPage = count % limit == 0 ? count / limit : count / limit + 1;
			result.put("totalPage", totalPage);
		}
		serialize(request, response, result);
		return null;
	}

	/**
	 * 
	 * @param request
	 *            还款功能
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/saveJkglHk")
	public ModelAndView saveJkglHk(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		SysUserDetails sysUserDetails = (SysUserDetails) SecurityContextHolder.getContext().getAuthentication()
				.getPrincipal();
		String dfrid = null;
		if (null != sysUserDetails) {
			dfrid = sysUserDetails.getGyh();
		} else {
			logger.info("saveJkglHk 未获取登陆用户名失 ");
			result.put("success", false);
			result.put("isAddSuc", false);
			result.put("msg", "获取登陆用户名失败,请重新登陆!");
			serialize(request, response, result);
			return null;
		}

		String id = RequestUtils.getStringParameter(request, "id");
		String gyh = RequestUtils.getStringParameter(request, "gyh");
		String sjhm = RequestUtils.getStringParameter(request, "sjhm");
		String refzqid = RequestUtils.getStringParameter(request, "refzqid");
		String refzqxmmc = RequestUtils.getStringParameter(request, "refzqxmmc");
		String refzqxmje = RequestUtils.getStringParameter(request, "refzqxmje");
		String yhkje = RequestUtils.getStringParameter(request, "yhkje");
		String yhzsy = RequestUtils.getStringParameter(request, "yhzsy");
		String yhsy = RequestUtils.getStringParameter(request, "yhsy");
		if (StringUtils.isEmpty(yhsy)) {
			yhsy = "0";
		}
		String kyje = RequestUtils.getStringParameter(request, "kyje");
		String djje = RequestUtils.getStringParameter(request, "djje");
		String zhye = RequestUtils.getStringParameter(request, "zhye");
		String hkzl = RequestUtils.getStringParameter(request, "hkzl");
		String hkdate = RequestUtils.getStringParameter(request, "hkdate");
		// 设置,发标时间超过规定日期,不可用此功能还款
		boolean isKyHkgn = jkglService.isKyHkgn(refzqid);
		if (!isKyHkgn) {
			logger.info("saveJkglHk 项目发标时间超过功能限制时间...");
			result.put("success", true);
			result.put("isAddSuc", false);
			result.put("msg", "此项目不可用本功能还款,请使用新版还款功能!");
			serialize(request, response, result);
			return null;
		}
		// 数据效验前,获取数据库数据,return:项目状态,应还款金额,账户可用金额 参数:ghy,id
		Map<String, Object> paramMap = new HashMap<>();
		paramMap = jkglService.isValidJkglHK(gyh, id);
		String hZt = (String) paramMap.get("ZT");
		BigDecimal hMjje = (BigDecimal) paramMap.get("MJJE");
		BigDecimal hKyje = (BigDecimal) paramMap.get("KYJE");
		// 根据还款种类和项目状态,判断是否可以还款?
		if ("01".equals(hkzl)) {
			params.put("sjhm", sjhm);
			params.put("refzqid", refzqid);
			boolean isHkBj = jkglService.isHkBj(params);
			if (isHkBj) {
				logger.info("saveJkglHk 项目本金已还 !");
				result.put("success", true);
				result.put("isAddSuc", false);
				result.put("msg", "项目本金已还,请勿重复还款!");
				serialize(request, response, result);
				return null;
			}
			// 应还款金额,可用金额是否为空?
			if (StringUtils.isNotEmpty(yhkje) && StringUtils.isNotEmpty(kyje)) {
				// 应还款与数据库数据相符?
				if ("01".equals(hkzl) && hMjje.doubleValue() != Double.valueOf(yhkje)) {
					logger.info("isValidJkglHK 应还款金额与数据库数值不符 ");
					result.put("success", true);
					result.put("isAddSuc", false);
					result.put("msg", "应还款金额与数据库数值不符,请重新操作");
					serialize(request, response, result);
					return null;
				} else if (NumberUtil.sub(hKyje.doubleValue(), Double.valueOf(kyje)) != 0) {// 可用余额与数据库数据相符?
					logger.info("isValidJkglHK 可用金额与数据库数值不符 ");
					result.put("success", true);
					result.put("isAddSuc", false);
					result.put("msg", "可用金额与数据库数值不符,请重新操作");
					serialize(request, response, result);
					return null;
				} else if (Double.valueOf(yhkje) <= 0 || Double.valueOf(kyje) < Double.valueOf(yhkje)) {// 应还款金额大于0,并小于等于可用余额?
					logger.info("saveJkglHk 应还款金额小于等于零或者可用金额小于应还款金额 ");
					result.put("success", true);
					result.put("isAddSuc", false);
					result.put("msg", "应还款金额小于等于零或者可用金额小于应还款金额");
					serialize(request, response, result);
					return null;
				}

			} else {
				logger.info("saveJkglHk 本次还款为空值 ");
				result.put("success", true);
				result.put("isAddSuc", false);
				result.put("msg", "本次还款为空值，请重新操作!");
				serialize(request, response, result);
				return null;
			}

		} else if ("02".equals(hkzl)) {
			if (!("0".equals(hZt) || "2".equals(hZt) || "3".equals(hZt) || "4".equals(hZt))) {
				logger.info("isValidJkglHK 项目状态为 " + hZt + "不可还收益");
				result.put("success", true);
				result.put("isAddSuc", false);
				result.put("msg", "项目状态不可还收益");
				serialize(request, response, result);
				return null;
			}
			// 应还款金额,可用金额,应还总收益,已还收益是否为空?
			if (StringUtils.isNotEmpty(yhkje) && StringUtils.isNotEmpty(kyje) && StringUtils.isNotEmpty(yhzsy)
					&& StringUtils.isNotEmpty(yhsy)) {
				// 应还款与数据库数据相符?
				if ("01".equals(hkzl) && hMjje.doubleValue() != Double.valueOf(yhkje)) {
					logger.info("isValidJkglHK 应还款金额与数据库数值不符 ");
					result.put("success", true);
					result.put("isAddSuc", false);
					result.put("msg", "应还款金额与数据库数值不符,请重新操作");
					serialize(request, response, result);
					return null;
				} else if (hKyje.doubleValue() != Double.valueOf(kyje)) {// 可用余额与数据库数据相符?
					logger.info("isValidJkglHK 可用金额与数据库数值不符 ");
					result.put("success", true);
					result.put("isAddSuc", false);
					result.put("msg", "可用金额与数据库数值不符,请重新操作");
					serialize(request, response, result);
					return null;
				} else if (Double.valueOf(yhkje) <= 0 || Double.valueOf(kyje) < Double.valueOf(yhkje)) {// 应还款金额大于0,并小于等于可用余额?
					logger.info("saveJkglHk 应还款金额小于等于零或者可用金额小于应还款金额 ");
					result.put("success", true);
					result.put("isAddSuc", false);
					result.put("msg", "应还款金额小于等于零或者可用金额小于应还款金额");
					serialize(request, response, result);
					return null;
				} else if (NumberUtil.sub(Double.valueOf(yhkje),
						NumberUtil.sub(Double.valueOf(yhzsy), Double.valueOf(yhsy))) > 0) {// 应还款金额小于等于应还剩余收益?
					logger.info("saveJkglHk 应还款金额不能大于应还剩余总收益 ");
					result.put("success", true);
					result.put("isAddSuc", false);
					result.put("msg", "本次还款不能大于应还剩余总收益，请重新输入!");
					serialize(request, response, result);
					return null;
				}

			} else {
				logger.info("saveJkglHk 本次还款为空值 ");
				result.put("success", true);
				result.put("isAddSuc", false);
				result.put("msg", "本次还款为空值，请重新操作!");
				serialize(request, response, result);
				return null;
			}
		}

		params.put("dfrid", dfrid);
		params.put("id", id);
		params.put("gyh", gyh);
		params.put("sjhm", sjhm);
		params.put("refzqid", refzqid);
		params.put("refzqxmmc", refzqxmmc);
		params.put("refzqxmje", refzqxmje);
		params.put("yhkje", yhkje);
		params.put("djje", djje);
		params.put("kyje", kyje);
		params.put("hkdate", hkdate);
		params.put("hkzl", hkzl);
		params.put("zhye", zhye);
		try {
			jkglService.saveJkglHk(params);
			result.put("success", true);
			result.put("isAddSuc", true);
			result.put("msg", "还款成功!");
		} catch (Exception e) {
			e.printStackTrace();
			logger.info("saveJkglHk 还款保存失败! " + e.getMessage());
			result.put("success", true);
			result.put("isAddSuc", false);
			result.put("msg", "还款失败!");
		}
		serialize(request, response, result);
		return null;
	}

	/**
	 * 
	 * @param request
	 *            查询借款人还款明细
	 * @param response
	 *            查询功能
	 * @return
	 * @throws Exception
	 */

	@RequestMapping("/queryHkmxList")
	public ModelAndView queryHkmxList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		SysUserDetails sysUserDetails = (SysUserDetails) SecurityContextHolder.getContext().getAuthentication()
				.getPrincipal();
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		Integer page = RequestUtils.getIntParameter(request, "page");// 新版本,传参
		if (null != page) {// 新版本,传参
			start = (page - 1) * limit;
		}
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		String xmmct = RequestUtils.getStringParameter(request, "xmmct");
		String zqid = RequestUtils.getStringParameter(request, "zqid");
		String state = RequestUtils.getStringParameter(request, "state");
		String hkzl = RequestUtils.getStringParameter(request, "hkzl");
		String query = RequestUtils.getStringParameter(request, "query");
		if (!"all".equals(query)) {
			params.put("sjhm", sysUserDetails.getGyh());
		}
		params.put("start", start);
		params.put("limit", limit);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("state", state);
		params.put("zqid", zqid);
		params.put("xmmct", xmmct);
		params.put("hkzl", hkzl);
		List<?> list = jkglService.queryHkmxList(params);
		int count = jkglService.getHkmxCount(params);
		if (null != page) {// 新版本,传参
			int totalPage = count % limit == 0 ? count / limit : count / limit + 1;
			result.put("totalPage", totalPage);
		}
		result.put("data", list);
		result.put("total", count);
		serialize(request, response, result);
		return null;
	}

	/**
	 * @作者 js
	 * @方法名 queryYhkList
	 * @功能 TODO 获取应还款列表
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryYhkList")
	public ModelAndView queryYhkList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		Integer page = RequestUtils.getIntParameter(request, "page");// 新版本,传参
		if (null != page) {// 新版本,传参
			start = (page - 1) * limit;
		}
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		String xmmc = RequestUtils.getStringParameter(request, "xmmc");
		String zqid = RequestUtils.getStringParameter(request, "zqid");
		String zt = RequestUtils.getStringParameter(request, "zt");
		String xmType = RequestUtils.getStringParameter(request, "xmType");
		String dfr = RequestUtils.getStringParameter(request, "dfr");
		params.put("start", start);
		params.put("limit", limit);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("xmmc", xmmc);
		params.put("zqid", zqid);
		params.put("zt", zt);
		params.put("xmType", xmType);
		params.put("dfr", dfr);
		List<?> list = jkglService.listYhkmx(params);
		int count = jkglService.getYhkmxCount(params);
		if (null != page) {// 新版本,传参
			int totalPage = count % limit == 0 ? count / limit : count / limit + 1;
			result.put("totalPage", totalPage);
		}
		result.put("data", list);
		result.put("total", count);
		serialize(request, response, result);
		return null;
	}

	@RequestMapping("exportYhkmxExcelMethod")
	public ModelAndView exportYhkmxExcelMethod(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JSONObject params = new JSONObject();
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		String xmmc = RequestUtils.getStringParameter(request, "xmmc");
		String zqid = RequestUtils.getStringParameter(request, "zqid");
		String zt = RequestUtils.getStringParameter(request, "zt");
		String xmType = RequestUtils.getStringParameter(request, "xmType");
		String dfr = RequestUtils.getStringParameter(request, "dfr");
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("xmmc", xmmc);
		params.put("zqid", zqid);
		params.put("zt", zt);
		params.put("xmType", xmType);
		params.put("dfr", dfr);

		List<Map<String, Object>> list = jkglService.listYhkmx(params);
		List<String[]> dataList = com.bringway.p2p.constant.ExcelHeadConstant.getYhkmxExportSearchResultData(list);

		// 导出数据到Excel文件
		List<ExcelSheet> sheetList = new ArrayList<ExcelSheet>();
		ExcelSheet sheet = new ExcelSheet();
		sheet.setBodyList(dataList);
		sheet.setSheetName(DateUtils.getCurrentDataStr() + "应还款本金和收益明细");
		sheetList.add(sheet);
		OutputStream os = response.getOutputStream();
		String fileName = DateUtils.getCurrentDataStr() + "应还款本金和收益明细表.xls";
		super.setDownLoadResponseHeader(request, response, fileName);
		response.setContentType("application/vnd.ms-excel");

		// 导出EXCEL
		ExcelOperUtil.writeExcel(sheetList, os);
		return null;
	}

	/**
	 * @作者 js
	 * @方法名 listMarryHk
	 * @功能 TODO 获取应还款项目列表
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("listMarryHk")
	public ModelAndView listMarryHk(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		String xmmc = RequestUtils.getStringParameter(request, "xmmc");
		String zqid = RequestUtils.getStringParameter(request, "zqid");
		String dfr = RequestUtils.getStringParameter(request, "dfr");
		params.put("start", start);
		params.put("limit", limit);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("xmmc", xmmc);
		params.put("zqid", zqid);
		params.put("dfr", dfr);
		List<Map<String, Object>> list = jkglService.listMarryHk(params);
		int count = jkglService.getMarryHkCount(params);
		result.put("data", list);
		result.put("total", count);
		serialize(request, response, result);
		return null;
	}

	/**
	 * @作者 js
	 * @方法名 listMarryYhk
	 * @功能 TODO 某一个项目的应还款-未匹配明细
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("listMarryYhk")
	public ModelAndView listMarryYhk(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		String xmid = RequestUtils.getStringParameter(request, "xmid");
		String username = RequestUtils.getStringParameter(request, "username");
		params.put("start", start);
		params.put("limit", limit);
		params.put("xmid", xmid);
		params.put("username", username);
		List<Map<String, Object>> list = jkglService.listMarryYhk(params);
		result.put("data", list);
		serialize(request, response, result);
		return null;
	}

	/**
	 * @作者 js
	 * @方法名 listMarryHedHk
	 * @功能 TODO 获取某一个项目的已还款-未匹配明细
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("listMarryHedHk")
	public ModelAndView listMarryHedHk(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		String xmid = RequestUtils.getStringParameter(request, "xmid");
		String username = RequestUtils.getStringParameter(request, "username");
		params.put("start", start);
		params.put("limit", limit);
		params.put("xmid", xmid);
		params.put("username", username);
		List<Map<String, Object>> list = jkglService.listMarryHedHk(params);
		result.put("data", list);
		serialize(request, response, result);
		return null;
	}

	/**
	 * @作者 js
	 * @方法名 saveMarryHk
	 * @功能 TODO 保存还款记录的匹配信息
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("saveMarryHk")
	public ModelAndView saveMarryHk(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();
		String id = RequestUtils.getStringParameter(request, "id");
		String hkId = RequestUtils.getStringParameter(request, "hkId");
		result.put("success", true);
		try {
			JSONObject saveMarryHk = jkglService.saveMarryHk(id, hkId);
			result.putAll(saveMarryHk);
		} catch (Exception e) {
			logger.error(e);
			result.put("res", "0");
			result.put("msg", "匹配失败!");
			serialize(request, response, result);
			return null;
		}
		serialize(request, response, result);
		return null;
	}

	/**
	 * @作者 js
	 * @方法名 listYhkAndDfInfo
	 * @功能 TODO 获取借款人待垫付列表
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("listYhkAndDfInfo")
	public ModelAndView listYhkAndDfInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		String startDateReal = RequestUtils.getStringParameter(request, "startDateReal");
		String endDateReal = RequestUtils.getStringParameter(request, "endDateReal");
		String xmType = RequestUtils.getStringParameter(request, "xmType");
		String xmmc = RequestUtils.getStringParameter(request, "xmmc");
		String kumc = RequestUtils.getStringParameter(request, "kumc");
		String hkzt = RequestUtils.getStringParameter(request, "hkzt");
		String username = RequestUtils.getStringParameter(request, "username");
		params.put("start", start);
		params.put("limit", limit);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("startDateReal", startDateReal);
		params.put("endDateReal", endDateReal);
		params.put("xmType", xmType);
		params.put("xmmc", URLDecoder.decode(xmmc));
		params.put("kumc", URLDecoder.decode(kumc));
		params.put("hkzt", hkzt);
		params.put("username", username);
		List<Map<String, Object>> list = jkglService.listYhkAndDfInfo(params);
		int count = jkglService.getYhkAndDfCount(params);
		Map<String, Object> sumJeMap = jkglService.getYhkAndDfSum(params);
		result.put("data", list);
		result.put("total", count);
		result.put("sumJe", sumJeMap);
		serialize(request, response, result);
		return null;
	}

	/**
	 * @作者 js
	 * @方法名 sendYzm
	 * @功能 TODO 发送垫付短信验证码
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/sendYzm")
	public ModelAndView sendYzm(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();
		SysUserDetails sysUserDetails = (SysUserDetails) SecurityContextHolder.getContext().getAuthentication()
				.getPrincipal();
		String dfrid = null;
		if (null != sysUserDetails) {
			dfrid = sysUserDetails.getGyh();
		}
		String dfgyh = RequestUtils.getStringParameter(request, "dfgyh");
		if (StringUtils.isEmpty(dfgyh) || !dfgyh.equals(dfrid)) {
			result.put("res", "0");
			result.put("msg", "登录信息有误,请重新登录!");
			serialize(request, response, result);
			return null;
		}
		String sjhm = RequestUtils.getStringParameter(request, "dfsjhm");
		if (StringUtils.isNotEmpty(sjhm)) {
			// p2p中是否存在此平台账户
			Map<String, Object> jbxxMap = jkglService.getJbxxInfo(dfgyh);
			if (MapUtils.isEmpty(jbxxMap) || !sjhm.equals(MapUtils.getString(jbxxMap, "SJHM"))
					|| !"平台账户".equals(MapUtils.getString(jbxxMap, "GWLX"))) {
				result.put("res", "0");
				result.put("msg", "无此平台账户,请联系管理员!");
				serialize(request, response, result);
				return null;
			}
			boolean isGetYzm = this.yzmService.getYzmCount(sjhm, "3");
			if (isGetYzm) {
				result.put("res", "0");
				result.put("msg", "获取验证码至少需间隔1分钟!");
			} else {
				String yzm = this.yzmService.getRandNum(6);
				String sms = yzm + "，还款垫付验证码，请勿向任何人泄露。";
				String sign = "红点e贷";
				boolean flag = this.yzmService.insertYzm(sjhm, yzm, "3");
				if (flag) {
					String inputline = MsgUtils.fsdx(sms, sign, sjhm);
					// 返回结果为‘0，20140009090990,1，提交成功’ 发送成功 具体见说明文档
					System.out.println("返回字符串 : " + inputline);
					boolean isSuc = inputline.split(",")[0].equals("0")
							&& (inputline.contains("提交成功") || inputline.contains("鎻愪氦鎴愬姛"));
					if (isSuc) {
						result.put("res", "1");
						result.put("msg", "发送成功!");
					} else {
						result.put("res", "0");
						result.put("msg", "发送失败!");
					}
				} else {
					result.put("res", "0");
					result.put("msg", "发送失败!");
				}
			}
		} else {
			result.put("res", "0");
			result.put("msg", "垫付手机号码不能为空!");
		}
		serialize(request, response, result);
		return null;
	}

	@RequestMapping("/saveDfInfo")
	public synchronized ModelAndView saveDfInfo(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JSONObject result = new JSONObject();
		result.put("success", true);
		SysUserDetails sysUserDetails = (SysUserDetails) SecurityContextHolder.getContext().getAuthentication()
				.getPrincipal();
		String dfrid = null;
		if (null != sysUserDetails) {
			dfrid = sysUserDetails.getGyh();
		}
		String yhkid = RequestUtils.getStringParameter(request, "S_ID");
		String yhbj = RequestUtils.getStringParameter(request, "S_YHBJ");
		String yhlx = RequestUtils.getStringParameter(request, "S_YHLX");
		String dfje = RequestUtils.getStringParameter(request, "S_DFJE");
		String dflx = RequestUtils.getStringParameter(request, "S_DFLX_VALUE");
		String gyh = RequestUtils.getStringParameter(request, "S_DFGYH");
		String sjhm = RequestUtils.getStringParameter(request, "S_DFSJHM");
		String dxyzm = RequestUtils.getStringParameter(request, "S_DXYZM");
		String jymm = RequestUtils.getStringParameter(request, "S_JYMM");
		if (StringUtils.isEmpty(yhkid) || StringUtils.isEmpty(yhbj) || StringUtils.isEmpty(yhlx)
				|| StringUtils.isEmpty(dfje) || StringUtils.isEmpty(dflx) || StringUtils.isEmpty(gyh)
				|| StringUtils.isEmpty(sjhm) || StringUtils.isEmpty(dxyzm) || StringUtils.isEmpty(jymm)) {
			result.put("res", "0");
			result.put("msg", "参数有误,请重新操作!");
			serialize(request, response, result);
			return null;
		}
		if (!gyh.equals(dfrid)) {
			result.put("res", "0");
			result.put("msg", "登录信息有误,请重新登录!");
			serialize(request, response, result);
			return null;
		}
		try {
			// 获取平台账户基本信息
			Map<String, Object> jbxxMap = jkglService.getJbxxInfo(gyh);
			if (MapUtils.isEmpty(jbxxMap) || !sjhm.equals(MapUtils.getString(jbxxMap, "SJHM"))
					|| !"平台账户".equals(MapUtils.getString(jbxxMap, "GWLX"))) {
				result.put("res", "0");
				result.put("msg", "无此平台账户,请联系管理员!");
				serialize(request, response, result);
				return null;
			}
			// 获取欲垫付的还款信息
			Map<String, Object> yhkInfo = jkglService.getYhkInfoById(yhkid);
			if (MapUtils.isEmpty(yhkInfo)
					|| (NumberUtil.sub(Double.valueOf(dfje), MapUtils.getDouble(yhkInfo, "yhhj")) != 0)
					|| !("1001".equals(MapUtils.getString(yhkInfo, "hkzt"))
							|| "1002".equals(MapUtils.getString(yhkInfo, "hkzt")))) {
				result.put("res", "0");
				result.put("msg", "垫付信息不匹配!");
				serialize(request, response, result);
				return null;
			}
			if (NumberUtil.sub(Double.valueOf(dfje), MapUtils.getDouble(jbxxMap, "KYJE")) > 0) {
				result.put("res", "0");
				result.put("msg", "账户可用余额:" + MapUtils.getDouble(jbxxMap, "KYJE") + ",不足!");
				serialize(request, response, result);
				return null;
			}
			// 验证交易密码
			if (!MD5Util.MD5Encode(jymm, "UTF-8").equals(MapUtils.getString(jbxxMap, "JYMM"))) {
				result.put("res", "0");
				result.put("msg", "交易密码不正确!");
				serialize(request, response, result);
				return null;
			}
			// 验证短信验证码
			boolean isEffectiveYzm = this.yzmService.isEffectiveYzm(sjhm, dxyzm, "3");
			if (!isEffectiveYzm) {
				result.put("res", "0");
				result.put("msg", "短信验证码超时!");
				serialize(request, response, result);
				return null;
			}
			boolean verifyYzm = this.yzmService.verifyYzm(sjhm, dxyzm, "3");
			if (!verifyYzm) {
				result.put("res", "0");
				result.put("msg", "短信验证码不正确!");
				serialize(request, response, result);
				return null;
			} else {
				// 操作垫付
				result=jkglService.saveDfInfo(yhkInfo, jbxxMap, dflx, gyh);
				result.put("success", true);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("res", "0");
			result.put("msg", "数据异常,请重试!");
			serialize(request, response, result);
			return null;
		}
		serialize(request, response, result);
		return null;
	}
}
